import sys
import re
from docx import Document

def extract_asn1(input_file, output_file):
    text = ""
    pattern_start = r'-- ASN1START'
    pattern_end = r'-- ASN1STOP'
    asn_start = False
    asn_end = False
    with open(input_file, 'r') as file:
        for line in file:
            result = re.match(pattern_end, line)
            if result:
                asn_end = True
                asn_start = False
                text += "-- ASN1STOP\n"

            result = re.match(pattern_start, line)
            if result:
                asn_start = True
                asn_end = False


            if asn_start == True and asn_end == False:
                text += line
                
    with open(output_file, "w") as f:
        f.write(text)
 
def convert_to_txt(filename):
    try:
        # 打开.doc文件
        document = Document(filename)
        file_name_without_extension = filename[:filename.rfind(".")]
        
        # 创建新的.txt文件并写入内容
        txt_file = open(file_name_without_extension + '.txt', 'w')
        for paragraph in document.paragraphs:
            txt_file.write(paragraph.text + '\n')
            
        
        # 调用函数进行测试
        extract_asn1(file_name_without_extension + '.txt', file_name_without_extension + '.asn')
    
    except Exception as e:
        print("转换过程中发生错误:", str(e))
 
# 获取命令行参数（包括文件名）
if len(sys.argv) > 1:
    filename = sys.argv[1]
else:
    print("未提供输入文件名")
    exit()

convert_to_txt(filename)



 

 

